package com.meteor.controller;

import com.github.pagehelper.PageInfo;
import com.meteor.pojo.Book;
import com.meteor.pojo.Comment;
import com.meteor.pojo.Type;
import com.meteor.service.BookService;
import com.meteor.service.CommentService;
import com.meteor.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookBrotherController {

    @Resource
    private BookService bookService;

    @Resource
    private TypeService typeService;

    @Resource
    private CommentService commentService;

    // 图书查询显示
    @GetMapping("/bookBrother")
    public String book(Model model) {
        PageInfo<Book> info = bookService.selectBook(1, null);
        List<Type> types = typeService.selectAllType();
        List<Book> books = info.getList();
        model.addAttribute("types",types);
        model.addAttribute("books",books);
        model.addAttribute("info", info);
        return "front/book";
    }


    // 图书查询（可模糊全局搜索）
    @RequestMapping("/bookBrotherPage")
    @ResponseBody
    public PageInfo<Book> selectBook(Integer pageNum,String bookName) {
        if(bookName.equals("")) {
            bookName = null;
        }
        PageInfo<Book> books = bookService.selectBook(pageNum, bookName);
        return books;
    }

     //进入每本书的详情页面 展示书的内容与评论
    @GetMapping("/bookDetail/{id}")
    public String bookDetail(@PathVariable Integer id, Model model) {
        Book book = bookService.selectBookById(id);
        model.addAttribute("book", book);
        List<Comment> comments = commentService.selectAllById(id);
        model.addAttribute("comments", comments);
        // 跳转到书籍详情页面
        return "front/bookDetail";
    }


}



















